package com.thx.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 手机号的相关处理类.
 *
 * @author 贾红磊
 */
public class PhoneUtil {

  static Logger log = LoggerFactory.getLogger(PhoneUtil.class);

  // 手机号正则表达式
  static String MOBILE_REQEX = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";

  // 中国移动手机号正则表达式，139、138、137、136、135、134、159、158、157（3G）、152、151、150、188（3G）、187（3G）;14个号段
  static String CMCC_REQEX = "^((13[4-9])|(15[^3,4,5,6,\\D])|(18[2,7,8]))\\d{8}$";

  static String UNICOM_REQEX = "";// 中国联通手机号正则表达式 TODO
  static String CT_REQEX = "";// 中国电信手机号正则表达式 TODO

  static Pattern MOBILE_P = Pattern.compile(MOBILE_REQEX);//
  static Pattern CMCC_P = Pattern.compile(CMCC_REQEX);
  static Pattern UNICOM_P = Pattern.compile(UNICOM_REQEX);
  static Pattern CT_P = Pattern.compile(CT_REQEX);

  public static enum Type {
    MOBILE, CMCC, UNICOM, CT
  }

  /**
   * 支持重新设置正则表达式.
   * <pre>
   *   PhoneUtil.setReqex("", PhoneUtil.Type.CMCC)
   * </pre>
   */
  public static void setReqex(String regex, Type type) {
    switch (type) {
      case MOBILE:
        MOBILE_REQEX = regex;
        MOBILE_P = Pattern.compile(MOBILE_REQEX);
        break;
      case CMCC:
        CMCC_REQEX = regex;
        CMCC_P = Pattern.compile(CMCC_REQEX);
        break;
      case UNICOM:
        UNICOM_REQEX = regex;
        UNICOM_P = Pattern.compile(UNICOM_REQEX);
        break;
      case CT:
        CT_REQEX = regex;
        CT_P = Pattern.compile(CT_REQEX);
        break;
      default:
        break;
    }
  }

  /**
   * 检查是否是中国移动手机号.
   * 139、138、137、136、135、134、159、158、157（3G）、152、151、150、188（3G）、187（3G）;14个号段
   *
   * @param mobile 待检查手机号
   */
  //
  public static boolean isCmccMobileNo(String mobile) {
    boolean isPhone = matches(mobile, CMCC_P);
    log.debug("isCmccMobileNo { mobile=" + mobile + "  isPhone=" + isPhone
        + " }");
    return isPhone;

  }

  /**
   * 检查是否是中国联通手机号.
   *
   * @param mobile 待检查手机号
   */
  public static boolean isUnicomMobileNo(String mobile) {
    boolean isPhone = matches(mobile, UNICOM_P);
    log.debug("isUnicomMobileNo { mobile=" + mobile + "  isPhone="
        + isPhone + " }");
    return isPhone;

  }

  /**
   * 检查是否是中国联电信机号.
   *
   * @param mobile 待检查手机号
   */
  public static boolean isCtMobileNo(String mobile) {
    boolean isPhone = matches(mobile, CT_P);
    log.debug("isCtMobileNo { mobile=" + mobile + "  isPhone=" + isPhone
        + " }");
    return isPhone;
  }

  private static boolean matches(String mobile, Pattern pattern) {
    if (mobile == null) {
      return false;
    }
    try {
      Matcher matcher = pattern.matcher(mobile);
      return matcher.matches();
    } catch (Exception ex) {
      return false;
    }
  }


  /**
   * 测试 main.
   */
  public static void main(String[] args) {

    String fileName = "fj_sms";

    File srcfile = new File("C:\\Users\\balance\\Desktop\\" + fileName
        + ".txt");
    File uniFile = new File("C:\\Users\\balance\\Desktop\\" + fileName
        + "_非移动.txt");
    File cmccFile = new File("C:\\Users\\balance\\Desktop\\" + fileName
        + "_移动.txt");

    try {
      BufferedReader reader = new BufferedReader(new FileReader(srcfile));

      BufferedWriter uniFileOutput = new BufferedWriter(new FileWriter(
          uniFile));
      BufferedWriter cmccFileOutput = new BufferedWriter(new FileWriter(
          cmccFile));

      String phone = null;

      while ((phone = reader.readLine()) != null) {
        if (isCmccMobileNo(phone)) {
          cmccFileOutput.write(phone + "\n");
        } else {
          uniFileOutput.write(phone + "\n");
        }
      }
      cmccFileOutput.close();
      uniFileOutput.close();
      reader.close();

    } catch (Exception ex) {
      ex.printStackTrace();
    }

  }

}
